<template>
  <div class="namelist-box">
    <div style="margin-bottom: 16px">
      <a-button type="primary" @click="openPop('add')">
        添加blog
      </a-button>
    </div>
    <a-table :columns="columns" :data-source="data" :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <span class="table-operation">
            <a-button type="link" @click="openPop('change', record.id)">修改</a-button>
            <a-button type="link" danger @click="delBlog(record.id)">删除</a-button>
          </span>
        </template>
      </template>
    </a-table>
    <a-pagination class="pagination" v-model:current="pgData.page" :total="pgData.total" :pageSize="pgData.per_page"
      @change="pageChange" show-less-items />
    <blogEdit :open="showAction" :editId="p_id" @close="closePop"></blogEdit>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from "vue";
import * as Api from "@req/api";
import { suc } from "@/utils";
import { Modal, message } from "ant-design-vue";
import blogEdit from "@/components/blogEdit/index.vue";

const pgData = ref({
  page: 1,
  per_page: 10,
  total: 0
})

const columns = [
  {
    title: "标题",
    dataIndex: "title",
  },
  {
    title: "作者",
    dataIndex: "author",
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    key: 'updated_at'
  },
  { title: "操作", dataIndex: "action", key: "action" },
];
const fileList = ref([]);
const data = ref([]);
const showAction = ref(false);
const p_id = ref(null);
const tableLoading = ref(false);


const pageChange = () => {
  getBlogList()
}

const handleCancel = () => {
  showAction.value = false;
}

const closePop = (type) => {
  showAction.value = false;
  p_id.value = null;
  type && getBlogList()
}

onMounted(() => {
  getBlogList()
});

const getBlogList = () => {
  tableLoading.value = true;
  Api.getBlogsList(pgData.value).then((res) => {
    if (suc(res.status)) {
      pgData.value.total = res.data.total_blogs;
      data.value = res.data.blogs;
    }
    tableLoading.value = false;
  });
}


const handleList = (list, level, pid) => {
  if (!list && list.length <= 0) return;
  for (const item of list) {
    if (pid) item.pid = pid;
    item.label = item.name;
    item.value = item.key;
    item.level = level;
    if (item.children.length <= 0) {
      delete item.children
    } else if (item.children) {
      item.children = handleList(item.children, level + 1, item.key);
    }
  }
  return list;
}

const openPop = (type, id) => {
  if (type != 'add') {
    p_id.value = id;
  } else {
    p_id.value = null;
  }
  showAction.value = true;
}

const delBlog = (id) => {
  Modal.confirm({
    title: "确认删除吗？",
    content: "删除后不可恢复，需重新添加",
    okText: "确认",
    cancelText: "取消",
    onOk() {
      Api.delBlog(id).then((res) => {
        if (suc(res.status)) {
          message.success(res.message)
          getBlogList()
        }
      })
    },
  });
}

</script>

<style lang="postcss" scoped>
.namelist-box {
  padding: 16px;

  ::v-deep .ant-image {
    border-radius: 5px;
    overflow: hidden;

    .ant-image-img {
      object-fit: cover;
    }
  }

  .pagination {
    margin-top: 20px;
    text-align: right;
  }
}
</style>